<?php
namespace app\agent\controller;
use think\Controller;
use think\Db;
use app\common\controller\AgentBaseController;

class Count extends AgentBaseController
{
	//统计
	public function user_form()
    {
    	$usersum = db('info_user')->where('mobile','<>','')->where('level',session('agent_user_id'))->count();
    	$this->assign('usersum',$usersum);
    	$usersum1 = db('info_user')->where('mobile','eq','')->where('level',session('agent_user_id'))->count();
    	$this->assign('usersum1',$usersum1);
    	return $this->fetch();
    }
        public function goods_list()
    {
		if(!empty(input('goods_no')))$where['a.goods_no'] = ['like','%'.input('goods_no').'%'];
		
		if(!empty(input('goods_name')))$where['a.goods_name'] = ['like','%'.input('goods_name').'%'];
		
		if(!empty(input('store_name')))$where['b.chain_id'] = input('store_name');
		
		if(!empty(input('type_id')))$where['a.trade_id'] = input('type_id');
			
		$where['a.agent_id'] = session('agent_id');
		$where['a.goods_state'] = ['<>',3];	
					
		if(!empty(input('store_name')))$map['a.chain_id'] = input('store_name');
		$map['a.agent_id'] = session('agent_id');
		//获取当前店铺商品
		$order_list = db('agent_syorder')
						->alias('a')
						->field('a.chain_id,b.goods_id,sum(b.price) as total_money,sum(b.num) as sales,sum(b.dk_price) as dk_price,sum(b.zs_price) as zs_price')
						->join('agent_syorder_items b','a.id = b.order_id','LEFT')			
						->where($map)
						->group('b.goods_id')
						->buildSql();
						
		$goods_list = db('agent_goods')
						->alias('a')
						->field('a.cost_price,b.total_money,a.id as goods_id,a.goods_no,a.stock,a.goods_name,a.sale_price,a.goods_state,b.sales,a.addtime,c.id,c.type_name,d.store_name,b.dk_price,b.zs_price')
						->join($order_list.' b','a.id = b.goods_id','LEFT')
						->join('agent_type c','a.trade_id = c.id','LEFT')
						->join('agent_store d','b.chain_id = d.id','LEFT')
						->where($where)
						->order('b.sales desc')
						->paginate(15,false,['query'=>request()->param()]);
		$this->assign('goods_list',$goods_list);
		
		//获取门店ID
		$agent_store = db('agent_store')->where('agent_id',session('agent_id'))->select();
		$this->assign('agent_store',$agent_store);		
		//获取商品分类类型
		$goods_type = db('agent_type')->where('agent_id',session('agent_id'))->select();
		$this->assign('goods_type',$goods_type);
		$this->assign('empty','<span class="empty">没有数据</span>');
		
    	return $this->fetch();
    }
    public function goods_form()
    {
    	if(request()->isPost()){
    		$param=input('post.');
			if(!empty($param['goods_no']))$where['a.goods_no'] = ['like','%'.$param['goods_no'].'%'];
			if(!empty($param['goods_name']))$where['a.goods_name'] = ['like','%'.$param['goods_name'].'%'];
    	}
		if(input('type_id'))$where['a.trade_id'] = input('type_id');
		$where['a.agent_id'] = session('agent_id');
		$where['a.goods_state'] = ['<>',3];
		//获取当前店铺商品
		$goods_list = db('agent_goods')
						->alias('a')
						->field('a.total_money,a.id as goods_id,a.goods_no,a.goods_name,a.sale_price,a.stock,a.goods_state,a.sales,a.addtime,b.id,b.type_name')
						->join('agent_type b','a.trade_id = b.id')
						->where($where)
						->order('a.sales desc')
						->paginate(15);
		$this->assign('goods_list',$goods_list);
		$stores=db("agent_store")->where("agent_id",session('agent_id'))->field("store_name,id")->select();
		$this->assign('stores',$stores);
		//获取商品分类类型
		$goods_type = db('agent_type')->where('agent_id',session('agent_id'))->select();
		$this->assign('goods_type',$goods_type);
		$this->assign('empty','<span class="empty">没有数据</span>');
		
    	return $this->fetch();
    }
    
    
    
     public function summary()
    {
    	$agent_id=session("agent_id");	
    	$where['agent_id']=session("agent_id");	
		$where1['agent_id']=session("agent_id");
		$mems=db('info_user')->where("tj_agent",$agent_id)->count();
		$today_users=db("info_user")->where("tj_agent",$agent_id)->whereTime('add_time', 'today')->cache(60)->count();
	   	$week_users=db("info_user")->where("tj_agent",$agent_id)->whereTime('add_time', 'week')->cache(60)->count();
	   	$month_users=db("info_user")->where("tj_agent",$agent_id)->whereTime('add_time', 'month')->cache(60)->count();
	    $this->assign('today_mems',$today_users?$today_users:0);
		$this->assign('week_mems',$week_users?$week_users:0);
		$this->assign('month_mems',$month_users?$month_users:0);
		$this->assign('mems',$mems?$mems:0);
		
		$list=db("log_paylog_copy")->where($where)->field("uid,balance,order_type,score,sy_type,payment_type")->select();
		$logs=db('log_paylog_copy')->where($where)->cache(30)->sum("balance");	   
	   	$today_logs=db("log_paylog_copy")->where($where)->whereTime('add_time', 'today')->cache(30)->sum("balance");
	   	$week_logs=db("log_paylog_copy")->where($where)->whereTime('add_time', 'week')->cache(30)->sum("balance");
	   	$month_logs=db("log_paylog_copy")->where($where)->whereTime('add_time', 'month')->cache(30)->sum("balance");
	   	$this->assign("logs",$logs?$logs:0);
	   	$this->assign("today_logs",$today_logs?$today_logs:0);
	   	$this->assign("week_logs",$week_logs?$week_logs:0);
	   	$this->assign("month_logs",$month_logs?$month_logs:0);
		
		$counts=db('log_paylog_copy')->cache(30)->count();
	    $today_counts=db("log_paylog_copy")->where($where)->whereTime('add_time', 'today')->cache(30)->count();
	   	$week_counts=db("log_paylog_copy")->where($where)->whereTime('add_time', 'week')->cache(30)->count();
	   	$month_counts=db("log_paylog_copy")->where($where)->whereTime('add_time', 'month')->cache(30)->count();
	   	$this->assign("counts",$counts?$counts:0);
	   	$this->assign("today_counts",$today_counts?$today_counts:0);
	   	$this->assign("week_counts",$week_counts?$week_counts:0);
	   	$this->assign("month_counts",$month_counts?$month_counts:0);
		
		$where['balance']=['<',0];
		$where['order_type']=['in','1,3,7'];
	    $today_scores=db("log_agent_score")->where($where)->whereTime('add_time', 'today')->cache(30)->sum("balance");
	   	$week_scores=db("log_agent_score")->where($where)->whereTime('add_time', 'week')->cache(30)->sum("balance");
	   	$month_scores=db("log_agent_score")->where($where)->whereTime('add_time', 'month')->cache(30)->sum("balance");
	   	$total_scores=db("log_agent_score")->where($where)->cache(30)->sum("balance");
		
		$where1['balance']=['>',0];
		$where1['order_type']=['in','1,3,7'];
	    $today_scores1=db("log_agent_score")->where($where1)->whereTime('add_time', 'today')->cache(30)->sum("balance");
	   	$week_scores1=db("log_agent_score")->where($where1)->whereTime('add_time', 'week')->cache(30)->sum("balance");
	   	$month_scores1=db("log_agent_score")->where($where1)->whereTime('add_time', 'month')->cache(30)->sum("balance");
	   	$total_scores1=db("log_agent_score")->where($where1)->cache(30)->sum("balance");
	   	$this->assign("today_scores",$today_scores?$today_scores:0);
	   	$this->assign("week_scores",$week_scores?$week_scores:0);
	   	$this->assign("month_scores",$month_scores?$month_scores:0);
		$this->assign("total_scores",$total_scores?$total_scores:0);
		$this->assign("today_scores1",$today_scores1?$today_scores1:0);
	   	$this->assign("week_scores1",$week_scores1?$week_scores1:0);
	   	$this->assign("month_scores1",$month_scores1?$month_scores1:0);
		$this->assign("total_scores1",$total_scores1?$total_scores1:0);
	
		
    	return $this->fetch();
    }
    public function detailed()
    {
		if(input('begin_time') && input('end_time')){
			$where['a.add_time'] = ['between time',[input('begin_time'),input('end_time')]];
		}
		if(input('store_name'))$where['a.chain_id'] = ['like','%'.input('store_name').'%'];
		if(input('syylist'))$where['a.clerk_id'] = input('syylist');
		if(input('pay_type'))$where['a.pay_type'] = input('pay_type');
		if(input('sy_type'))$where['a.sy_type'] = input('sy_type');
		if(input('state')){
			$where['a.state'] = input('state');
		}else{
			$where['a.state'] = 2;
		}
		if(input('order_sn'))$where['a.order_sn'] = ['like','%'.input('order_sn').'%'];

    	$where['a.agent_id'] = session('agent_id');	
		$where['a.order_type'] = ['not in','3,4,5'];		
    	$paylog = db('log_paylog')
    	->alias('a')
		->join('agent_store w','a.agent_id = w.agent_id and a.chain_id = w.id','LEFT')
		->field('a.*,w.store_name')
    	->where($where)
		->order('a.add_time desc')
    	->paginate(10,false,['query'=>request()->param()]);
		$this->assign('paylog',$paylog);

		//消费金额
		$xfprice = db('log_paylog')->alias('a')->where($where)->sum('a.origin_price');
		$this->assign('xfprice',$xfprice);
		
		//实际金额
		$sjprice = db('log_paylog')->alias('a')->where($where)->sum('a.balance');
		$this->assign('sjprice',$sjprice);
		
		//赠送
		$zsscore = db('log_paylog')->alias('a')->where($where)->where('a.payment_type',1)->sum('a.agent_score');
		$this->assign('zsscore',$zsscore);
		
		//抵扣
		$dkscore = db('log_paylog')->alias('a')->where($where)->where('a.payment_type',2)->sum('a.agent_score');
		$this->assign('dkscore',$dkscore);
		
		//获取门店ID
		$agent_store = db('agent_store')->where('agent_id',session('agent_id'))->select();
		$this->assign('agent_store',$agent_store);
		
		//获取收银员
		$clerk_list = db('agent_clerk')->where('agent_id',session('agent_id'))->select();
		$this->assign('clerk_list',$clerk_list);
		$this->assign('empty','<span class="empty">没有数据</span>');
		
    	return $this->fetch();
    }

 	//明细导出
    public function detail_dc(){
		if(input('begin_time') && input('end_time')){
			$where['a.add_time'] = ['between time',[input('begin_time'),input('end_time')]];
		}
		if(input('store_name'))$where['a.chain_id'] = ['like','%'.input('store_name').'%'];
		if(input('syylist'))$where['a.clerk_id'] = input('syylist');
		if(input('pay_type'))$where['a.pay_type'] = input('pay_type');
		if(input('sy_type'))$where['a.sy_type'] = input('sy_type');
		if(input('state')){
			$where['a.state'] = input('state');
		}else{
			$where['a.state'] = 2;
		}
		if(input('order_sn'))$where['a.order_sn'] = ['like','%'.input('order_sn').'%'];

    	$where['a.agent_id'] = session('agent_id');	
		$where['a.order_type'] = ['not in','3,4,5'];	
    	$list = db('log_paylog')
    	->alias('a')
		->join('agent_store w','a.agent_id = w.agent_id and a.chain_id = w.id','LEFT')
		->field('a.*,w.store_name')
    	->where($where)
		->order('a.add_time desc')
    	->select();

		$str ="订单号,门店,项目,支付方式,消费金额,实际金额,鼓励金,日期"."\n";
        $str = iconv('utf-8','gbk',$str);
        foreach($list as $k=>$row){
        	$order_sn = iconv('utf-8','gbk',$row['order_sn'].'	');
			$add_time = iconv('utf-8','gbk',$row['add_time'].'	');
			$store_name = iconv('utf-8','gbk',$row['store_name']); 
			$message = iconv('utf-8','gbk',$row['message'].'	'); 
			
			if($row['pay_type']==1){
				$payname = '余额';
			}elseif($row['pay_type']==2) {
				$payname = '微信';
			}elseif($row['pay_type']==3) {
				$payname = '支付宝';
			}else{
				$payname = '银联';
			}
			$pay_type = iconv('utf-8','gbk',$payname);
			$xfprice = iconv('utf-8','gbk','	'.$row['origin_price']);
			$sjprice = iconv('utf-8','gbk','	'.$row['balance']);
			$score = iconv('utf-8','gbk','	'.$row['agent_score']);
			$str .= $order_sn.",".$store_name.",".$message.",".$pay_type.",".$xfprice.",".$sjprice.",".$score.",".$add_time.","."\n"; //用引文逗号分开 
        } 

	  	$filename ='明细列表.csv';   //设置文件名  
    	
        header("Content-type:text/csv"); 
        header("Content-Disposition:attachment;filename=".$filename); 
        header('Cache-Control:must-revalidate,post-check=0,pre-check=0'); 
        header('Expires:0'); 
        header('Pragma:public'); 
        echo $str; 
        exit;
	}

    public function syylist()
    {
    	//获取收银员
		$data = db('agent_clerk')->where('chain_id',input('id'))->select();
		return json(["data"=>$data]);
    }
    
    
    
    
    
}
